package com.xyzh05.webapp.action;

import java.io.File;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Preparable;
import com.xyzh05.model.Attachment;
import com.xyzh05.model.Comment;
import com.xyzh05.model.ProgressStatistics;
import com.xyzh05.service.AttachmentManager;
import com.xyzh05.service.CommentManager;

public class CommentAction extends UploadFileAction implements Preparable {
	private static final long serialVersionUID = 3361645611972520226L;

	private CommentManager commentManager;
	private AttachmentManager attachmentManager;
	private List<Comment> comments;
	private Comment comment;
	private List<Attachment> attachments;
	private List<Object[]> statisList;
	private Attachment attachment;
	private ProgressStatistics progres;
	private Long id;
	private Long ownerId;
	private Long ownerType;
	private String content;
	private String inputPath;
	private String fileName;
	private Integer resultCode;
	private Long vipServiceId;

	/**
	 * Grab the entity from the database before populating with request
	 * parameters
	 */
	public void prepare() {
	}

	public String list() {
		comments = commentManager.getAllByTime(ownerId, ownerType);
		return SUCCESS;
	}

	public String delete() {
		return SUCCESS;
	}

	public String edit() {
		return SUCCESS;
	}

	public String save() {
		try {
			comment = new Comment();
			comment.setContent(content);
			comment.setOwnerId(ownerId);
			comment.setUser(getCurrentUser());
			comment.setStatus((short) 0);
			comment.setReplyTime(new Date());
			comment.setOwnerType(ownerType);
			comment = commentManager.save(comment);
		} catch (Exception e) {
			log.error("发表留言失败!", e);
		}
		File[] srcFiles = super.getUpload();
		if (srcFiles != null) {
			try {
				// 上传文件
				uploadFiles();
				attachment = new Attachment();
				attachment.setName(uploadFileName[0]);
				attachment.setPath(targetPath[0]);
				attachment.setUploadTime(new Date());
				attachment.setUploadUser(getCurrentUser().getId());
				attachment.setType(uploadContentType[0]);
				attachment.setOwnerId(comment.getId());
				attachment.setOwnerType(ownerType);
				attachment.setSize(Long.valueOf(fileSize[0]));
				attachmentManager.save(attachment);
			} catch (Exception e) {
				log.error("附件上传失败!", e);
			}
		}
		comments = commentManager.getAllByTime(ownerId, ownerType);
        return SUCCESS;
    }
    
    public InputStream getInputStream() throws Exception{
    	 return ServletActionContext.getServletContext().getResourceAsStream(inputPath + "/" + fileName);
    }

	public String commentOutline() {	
		statisList = commentManager.findStatistics(vipServiceId);
		progres = new ProgressStatistics(); 
		for (Object[] obj : statisList) {
			Boolean bool = (Boolean) obj[2];
			int l = Integer.parseInt(obj[0] + "");
			if (bool) {
				switch (l) {
				case 1:
					progres.setPSY(Long.valueOf(obj[1].toString()));
					break;
				case 2:
					progres.setRLY(Long.valueOf(obj[1].toString()));
					break;
				case 3:
					progres.setCVY(Long.valueOf(obj[1].toString()));
					break;
				case 11:
					progres.setESY(Long.valueOf(obj[1].toString()));
					break;
				default:
					break;
				}
			} else {
				switch (l) {
				case 1:
					progres.setPSN(Long.valueOf(obj[1].toString()));
					break;
				case 2:
					progres.setRLN(Long.valueOf(obj[1].toString()));
					break;
				case 3:
					progres.setCVN(Long.valueOf(obj[1].toString()));
					break;
				case 11:
					progres.setESN(Long.valueOf(obj[1].toString()));
					break;
				default:
					break;
				}
			}
		}
		comments = commentManager.getTopNum(vipServiceId.intValue(), 0, 3);			
		return SUCCESS;
	}

	public CommentManager getCommentManager() {
		return commentManager;
	}

	public void setCommentManager(CommentManager commentManager) {
		this.commentManager = commentManager;
	}

	public AttachmentManager getAttachmentManager() {
		return attachmentManager;
	}

	public void setAttachmentManager(AttachmentManager attachmentManager) {
		this.attachmentManager = attachmentManager;
	}

	public List<Comment> getComments() {
		return comments;
	}

	public void setComments(List<Comment> comments) {
		this.comments = comments;
	}

	public Comment getComment() {
		return comment;
	}

	public void setComment(Comment comment) {
		this.comment = comment;
	}

	public List<Attachment> getAttachments() {
		return attachments;
	}

	public void setAttachments(List<Attachment> attachments) {
		this.attachments = attachments;
	}

	public Attachment getAttachment() {
		return attachment;
	}

	public void setAttachment(Attachment attachment) {
		this.attachment = attachment;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getOwnerId() {
		return ownerId;
	}

	public void setOwnerId(Long ownerId) {
		this.ownerId = ownerId;
	}

	public Long getOwnerType() {
		return ownerType;
	}

	public void setOwnerType(Long ownerType) {
		this.ownerType = ownerType;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getInputPath() {
		return inputPath;
	}

	public void setInputPath(String inputPath) {
		this.inputPath = inputPath;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public Integer getResultCode() {
		return resultCode;
	}

	public void setResultCode(Integer resultCode) {
		this.resultCode = resultCode;
	}

	public Long getVipServiceId() {
		return vipServiceId;
	}

	public void setVipServiceId(Long vipServiceId) {
		this.vipServiceId = vipServiceId;
	}

	public List<Object[]> getStatisList() {
		return statisList;
	}

	public void setStatisList(List<Object[]> statisList) {
		this.statisList = statisList;
	}

	public ProgressStatistics getProgres() {
		return progres;
	}

	public void setProgres(ProgressStatistics progres) {
		this.progres = progres;
	}

}